home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / misc~1 / 5 / osspascl / file.doc < prev    next >
Text File  |  1985-11-19  |  9KB  |  258 lines

  1.  
  2.  
  3.  
  4.  
  5. Disk File Functions                                                     Page 1
  6.  
  7.                              Disk File Operations
  8.  
  9. There  may  be  times when you want to perform operations not supported by the
  10. standard Personal Pascal I/O library.  Until such time as equivalent  routines
  11. are  added  to the Personal Pascal library, you can use direct GEMDOS calls to
  12. achieve the results you want.  For all calls described in this section,  we're
  13. going to assume the following TYPE declarations are present in your program:
  14.  
  15.   TYPE
  16.     Path_Chars = PACKED ARRAY [ 1..80 ] OF Char ;
  17.  
  18. Notice  that  the  maximum  length of a GEMDOS pathname is 80 characters.  The
  19. reason we are using a packed array and not a Pascal STRING type is that GEMDOS
  20. is expecting path and file names in the "C language" string format.  In  other
  21. words,  GEMDOS  normally  wants  a string in which the first byte is the first
  22. data character, and it expects the string to be  terminated  by  a  zero  byte
  23. (Chr(  0  ),  in  Pascal).  You can't pass a Pascal STRING to GEMDOS directly,
  24. since the first character in a Pascal string (s[0]) is the length byte for the
  25. string, and the string is not null-terminated.  In  order  to  pass  a  Pascal
  26. string  to  GEMDOS,  you  have  to copy it into a "C-type" string by calling a
  27. procedure like the following:
  28.  
  29.   PROCEDURE Make_Path( VAR ps : Str255 ; VAR cs : Path_Chars ) ;
  30.  
  31.     VAR
  32.       i : Integer ;
  33.  
  34.     BEGIN
  35.       FOR i := 1 TO Length( ps ) DO
  36.         cs[i] := ps[i] ;
  37.       cs[ length(ps)+1 ] := Chr(0) ;
  38.     END ;
  39.  
  40. Now that we know what type of names to pass to GEMDOS,  we  can  get  on  with
  41. presenting the calls you can use:
  42.  
  43.  
  44. Create and open a file.
  45.  
  46. Sometimes,  you  may  want  open  a  file  with special properties that Pascal
  47. doesn't support.  For this purpose, you can use the following routine:
  48.  
  49. FUNCTION f_create( VAR name : Path_Chars ; attributes : Integer ) : Integer ;
  50.   GEMDOS( $3c ) ;
  51.  
  52. This call creates a new  file  with  the  specified  name  and  the  specified
  53. attributes.    The  bits  in  the  attributes  parameter  have  the  following
  54. assignments:
  55.  
  56. bit  meaning
  57. ---  -------
  58. $01  file is read-only
  59. $02  file is hidden from directory search
  60. $04  file is a system file, hidden from directory search
  61. $08  file contains a volume label in the first 8 data bytes
  62.  
  63. The return value is a valid GEMDOS file handle, if greater than  or  equal  to
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. Disk File Functions                                                     Page 2
  72.  
  73. zero,  or  an  error  number, if negative.  You should use this call to open a
  74. file for output, if you want to open a new file, or if you want to first erase
  75. the previous contents.  If you want to write  to  an  existing  file,  without
  76. erasing the contents, use the f_open call, below.
  77.  
  78.  
  79. Open a file.
  80.  
  81. You  might  also  want  to  open  an  existing  file  (or one you created with
  82. f_create) without using the built-in procedure Reset.  You can use this GEMDOS
  83. call:
  84.  
  85. FUNCTION f_open( VAR name : Path_Chars ; mode : Integer ) : Integer ;
  86.   GEMDOS( $3d ) ;
  87.  
  88. Use this call to open a file for reading, writing, or updating.  If  you  want
  89. to open a file for writing, but you want to first erase the previous contents,
  90. use the f_create call, instead.  The valid values for mode are:
  91.  
  92. 0  open for reading only
  93. 1  open for writing only
  94. 2  open for reading or writing
  95.  
  96. The  return  value is a GEMDOS handle, if greater than or equal to zero, or an
  97. error number, if negative.  Notice that this call does not have a parameter to
  98. specify the  attributes  of  the  file.   Those  attributes  are  set  by  the
  99. f_create  call  and  are  not changed by this call.  If you want to change the
  100. attributes of a file, you can use the f_attrib call, below.
  101.  
  102.  
  103. Close an open file.
  104.  
  105. If you used f_create or f_open to ready a file for access, you should use  the
  106. following  call  to  close  it  when you're finished reading or writing to the
  107. file:
  108.  
  109. FUNCTION f_close( handle : Integer ) : Integer ;
  110.   GEMDOS( $3e ) ;
  111.  
  112. The parameter handle should be the same as that returned  by  the  appropriate
  113. open  call.   Zero  is  returned,  if  the  file was closed successfully, or a
  114. negative error number, otherwise.
  115.  
  116.  
  117. Read bytes from a file.
  118.  
  119. Pascal supports reading from and writing to files one item at  a  time,  where
  120. the  size  of the item is the size of the file pointer variable.  Occasionally
  121. you may want to read or write in larger chunks, especially if your  item  size
  122. is  small,  since  GEMDOS  isn't  very  fast  for  single-byte transfers.  The
  123. following call allows you to read a block of characters into memory:
  124.  
  125. FUNCTION f_read( handle : Integer ; count : Long_Integer ; VAR buf :  Buf_Type
  126. ) : Long_Integer ;
  127.   GEMDOS( $3f ) ;
  128.  
  129. This  call  reads  an  arbitrary  number  of  bytes from a file into a desired
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. Disk File Functions                                                     Page 3
  138.  
  139. buffer.  The number of bytes actually read is returned, if  the  function  was
  140. successful,  or  a  negative error number, if something went wrong.  Note that
  141. the number of bytes actually read may be shorter  than  the  number  of  bytes
  142. requested,  if  the  end-of-file position was reached.  The Buf_Type mentioned
  143. above may be almost any type.  For example, to read 100 two-byte  values  into
  144. an array, you might use a program segment like this:
  145.   TYPE
  146.     Hundred_Integers = ARRAY [ 1..100 ] OF Integer ;
  147.  
  148.   VAR
  149.     a : Hundred_Integers ;
  150.     bytes_read : Long_Integer ;
  151.  
  152.   PROCEDURE  f_read(  handle  :  Integer  ;  count  : Long_Integer ; VAR buf :
  153. Hundred_Integers ) : Long_Integer ;
  154.   GEMDOS( $3f ) ;
  155.  
  156.   BEGIN
  157.     bytes_read := f_read( handle, 200, a ) ;
  158.   END ;
  159.  
  160. Note that 200 was passed as the number of bytes to read, since we  wanted  100
  161. two-byte values!
  162.  
  163. The  handle  parameter should be that value returned by either the f_create or
  164. f_open call.  If you want to use the f_read call to read from a file which was
  165. opened using the built-in procedure Reset, you can use the function Handle  to
  166. find  out the handle associated with the file.  If you are reading from a file
  167. just opened using Reset, you must be aware, however, that the first  item  has
  168. already been read from the file and put into the file buffer variable.
  169.  
  170. Write bytes to a file.
  171.  
  172. Similarly,  you may want to write an arbitrary number of bytes to a file.  The
  173. following call supports block writing:
  174.  
  175. FUNCTION f_write( handle : Integer ; count : Long_Integer ; VAR buf : Buf_Type
  176. ) : Long_Integer ;
  177.   GEMDOS( $40 ) ;
  178.  
  179. This call is the counterpart of the f_read function described above.  It takes
  180. an arbitrary number of bytes from a buffer and outputs them  to  a  previously
  181. opened  file.   The  handle  parameter  must  be  that which was returned by a
  182. previous f_open or f_create call.  You can also use the Handle function to get
  183. the handle of a file which was opened using the  Rewrite  built-in  procedure.
  184. The value returned by f_write is the number of bytes written, if the operation
  185. was  successful,  or  a  negative  error number.  In general, if the number of
  186. bytes returned does not equal the number requested, something went wrong!
  187.  
  188.  
  189. Delete a file.
  190.  
  191. There is no standard procedure in Pascal to remove a file from a disk,  so  if
  192. you want to erase files, you need the following call:
  193.  
  194. FUNCTION f_delete( VAR name : Path_Chars ) : Integer ;
  195.   GEMDOS( $41 ) ;
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203. Disk File Functions                                                     Page 4
  204.  
  205.  
  206. Zero  is  returned,  if  the delete was successful, or a negative error value,
  207. otherwise.
  208.  
  209.  
  210. Seek within a file.
  211.  
  212. Personal Pascal supports random access to files using the built-in  procedures
  213. Get,  Put, and Seek.  If you want to use instead the underlying GEMDOS routine
  214. to position within a file, here it is:
  215.  
  216. FUNCTION  f_seek(  offset  :  Long_Integer  ;  handle,  mode  :  Integer  )  :
  217. Long_Integer ;
  218.   GEMDOS( $42 ) ;
  219.  
  220. Use  this  call  to  point  to  a particular byte position within a file.  The
  221. offset parameter specifies the desired byte position, and the  mode  parameter
  222. specifies which file position the offset parameter is relative to:
  223.  
  224. mode  relative to
  225. ----  -----------
  226.   0   the beginning of the file
  227.   1   the current location
  228.   2   the end of the file
  229.  
  230. The  offset  parameter  is  signed,  so  you could, for example, move 10 bytes
  231. backwards in the file by specifying offset and mode parameters of -10  and  1,
  232. respectively.
  233.  
  234.  
  235. Get/Set file attributes.
  236.  
  237. As  mentioned  above,  the  f_create  call  sets  a  file's attributes.  These
  238. attributes are never changed when the file is  subsequently  opened.   If  you
  239. ever  want  to  change  the attributes of a file, you should use the following
  240. call:
  241.  
  242. FUNCTION f_attrib( VAR name : Path_Chars ; mode,  attributes  :  Integer  )  :
  243. Integer ;
  244.   GEMDOS( $43 ) ;
  245.  
  246. The  mode  parameter specifies whether to get the file attributes, if 0, or to
  247. set the attributes, if 1.  The attributes parameter is specified in  the  same
  248. way as for the f_create call, above, with the following two additions:
  249.  
  250. bit  meaning
  251. ---  -------
  252. $10  file is a subdirectory
  253. $20  file is written and closed correctly.
  254.  
  255. These two attributes only refer to subdirectories.
  256.  
  257.  
  258. Press <CR> to continue: